<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="addGoodsClass">添加</el-button>
        </el-col>
      </el-row>
      <tree-table  class="treeTable" :data="cateList" :columns="columns" :expand-type="false" show-index :show-row-hover="false" border :selection-type="false" :index-text="'#'">
        <template slot="isOk"  slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted === false" style="color: lightgreen;"></i>
          <i class="el-icon-error" v-else style="color: red;"></i>
        </template>
        <template slot="order"  slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level === 1">二级</el-tag>
          <el-tag type="warning" v-else>三级</el-tag>
        </template>
        <template slot="operation">
          <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
        </template>
      </tree-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="queryParam.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryParam.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="400"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      title="添加分类"
      :visible.sync="addDialogVisible"
      width="50%"
    >
      <el-form :rules="rules" label-width="80px" :model="addForm" ref="addFormRef">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="分类层级">
          <el-cascader
            v-model="selectdIds"
            :options="parentCateList"
            :props="props"
            change-on-select
            clearable
            expand-trigger="hover"
            @change="handleChange"></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="clearForm">取 消</el-button>
    <el-button type="primary" @click="confirm">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'cate',
  data() {
    return {
      selectdIds: [],
      addDialogVisible: false,
      props: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      queryParam: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      cateList: [],
      parentCateList: [],
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isOk'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'operation'
        }
      ],
      addForm: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      },
      rules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      }

    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get('categories', this.queryParam)
      if (res.meta.status !== 200) {
        return this.$message.error('获取分类列表失败')
      }
      this.cateList = res.data
    },
    handleSizeChange() {

    },
    handleCurrentChange() {

    },
    addGoodsClass() {
      this.getParentClasses()
      this.addDialogVisible = true
    },
    async getParentClasses() {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) {
        return this.$message.error('获取父级分类列表失败')
      }
      this.parentCateList = res.data
    },
    handleChange() {
      const len = this.selectdIds.length
      if (len > 0) {
        this.addForm.cat_pid = this.selectdIds[len - 1]
        this.addForm.cat_level = len
      } else {
        this.addForm.cat_pid = 0
        this.addForm.cat_level = 0
      }
    },
    async confirm() {
      this.$refs.addFormRef.validate(validate => {
        if (!validate) {
          return
        }
      })
      const { data: res } = await this.$http.post('categories', this.addForm)
      if (res.meta.status !== 201) {
        return this.$message.error('添加商品分类失败')
      }
      this.$message.success('添加分类成功')
      this.getCateList()
      this.addDialogVisible = false
      console.log(this.addForm)
    },
    clearForm() {
      this.$refs.addFormRef.resetFields()
      this.addForm.cat_level = 0
      this.addForm.cat_pid = 0
      this.selectdIds = []
      this.addDialogVisible = false
    }
  }
}
</script>

<style scoped>
  .treeTable {
    margin-top: 15px;
  }
</style>
